package com.young.manager.controller;

import com.young.common.interf.IBaseController;
import com.young.common.model.ResultData;
import com.young.manager.model.SysMenu;
import com.young.manager.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("sysMenu")
@RestController
@Slf4j
@RequiredArgsConstructor
public class SysMenuController implements IBaseController {

    private final SysMenuService sysMenuService;

    private final HttpServletRequest request;

    @GetMapping
    ResultData<?> querySysMenu(@RequestParam(required = false) String name, @RequestParam(required = false) String mark, @RequestParam(required = false) Long parentId) {
        return success(sysMenuService.queryList(name, mark, parentId));
    }

    @GetMapping("tree")
    ResultData<?> queryMenuTree() {
        return success(sysMenuService.queryMenuTree());
    }

    @GetMapping("role")
    ResultData<?> queryRoleMenus(@RequestParam Long roleId, @RequestParam(required = false) String column) {
        return success(sysMenuService.queryRoleMenuList(roleId, column));
    }

    @GetMapping("user")
    ResultData<?> queryUserMenus() {
        return success(sysMenuService.queryUserMenuList(request.getHeader("authUserId")));
    }

    @PostMapping("insert")
    ResultData<?> insertSysMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.insertSysMenu(sysMenu);
        return success();
    }

    @PostMapping("update")
    ResultData<?> updateSysMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateSysMenu(sysMenu);
        return success();
    }

    @PostMapping("delete")
    ResultData<?> deleteSysMenu(@RequestParam Long id) {
        sysMenuService.deleteSysMenu(id);
        return success();
    }

}
